<?php 

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all functions related to ADMIN Control Panel.
  Author: Yashpal
  Usage:  	call admin/{functioname}
			

  Function name : 
  Grab_Cats          -> This function will find all details of categories.
  				        Give an 2D array containing all values of categories.
  SelectCategories   -> This function will give Select List HTML Code of categories.
  						HTMl Select with categories value
  CreateLicategories -> This function will give LI List HTML Code of categories.
						HTMl li version with categories valu
 

*/
 
class Admin extends Controller {

	function Admin()
	{
		parent::Controller();	
		//check for admin login session 
		//admin group -> ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4
		//$login = $this->session->userdata('blogmer_admin_logged_in');	
		$login = $this->session->userdata('blogmer_admin_logged_in');
		if(!$login)
		{		 redirect('login/admin_login','refresh'); }	
	}
	
	
	function index()
	{
		$data['title']="Admin Dashboard - Blogmer";
		$data['content']=$this->load->view('admin/dashboard',$data,TRUE);
		$this->load->view('admin/main',$data);
	}
	
	function Logout()
	{
			$this->session->sess_destroy();
			$info['title']="Information Page";
			$info['header']="Information";
			$info['message']="You are successfully logout.";
			$info['url']="/blog/";
			$info['time']="2";
			
		  	$this->load->view('info',$info);
		
	}
	
	
	/**************************************************************************************/
	/***************************** Settings functions *************************************/
	
	
	function GeneralSettings()
	{
		$rules['BLOGName'] = "required";
		$rules['BLOGTitle'] = "required";
		$rules['BLOGMaintainedBy'] = "required";
		$this->validation->set_rules($rules);

		$fields['BLOGName'] = 'BLOG Name';
		$fields['BLOGTitle'] = 'BLOG Title';
		$fields['BLOGMaintainedBy'] = 'BLOG Maintained By';
		$fields['GuestView'] = 'Guest View';
		$fields['EnableMORE'] = 'Enable MORE';
		$fields['ShowRecentComments'] = 'Show Recent Comments';
		$fields['ShowRecentPosts'] = 'Show Recent Posts';
		$fields['ShowCalendar'] = 'Show Calendar';
		$fields['EnableRegistration'] = 'Enable Registration';
		$fields['EnableGuestComment'] = 'Enable Guest Comment';
		$fields['SendRegistrationMessage'] = 'Send Registration Message';
		
		$this->validation->set_fields($fields);
		if ($this->validation->run()) 
		 { 	$this->Settings_model->SaveGeneralSettings();}
		else 
		{
            $data['generalsettings']=$this->Settings_model->GetGeneralSetting();
			$data['title']="General Settings - Blogmer ";
			$data['content']=$this->load->view('admin/settings_general',$data,TRUE);
			$this->load->view('admin/main',$data);
        }
	}
	
	function AdvancedSettings()
	{
		$data['title']="Advanced Settings - Blogmer";
		$rules['NewUserDefaultRole'] = "required";
		$this->validation->set_rules($rules);

		$fields['NewUserDefaultRole'] = 'New User Default Role';
		$this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{     	echo $this->Settings_model->SaveAdvancedSettings();        } 
		else 
		{
       		$data['advancedsettings']=$this->Settings_model->GetAdvancedSetting();
			$data['title']="Advanced Settings - Blogmer";
			$data['content']=$this->load->view('admin/settings_advance.php',$data,TRUE);
			$this->load->view('admin/main',$data);
        }
	}

	/*********************** end of settings functions ************************************/
	
	/**************************************************************************************/
	/***************************** Posts functions ****************************************/
	
	
	
	function CreateBlog()
	{
		//Some posted items are required
		$rules['Title'] = "required";
		$rules['Content'] = "required";
		$rules['Status'] = "required";
		$rules['Author'] = "required";
		$rules['AuthorEmail'] = "trim|required|valid_email|max_length[100]";
		$rules['blog_date']="required";
		$rules['blog_time']="required";
		
		$this->validation->set_rules($rules);
			
		//Text for posted items. 
		$fields['Title'] = 'Post Title';
		$fields['Content'] = 'Post Content';
		$fields['Author'] = 'Author Name';
		$fields['AuthorEmail'] = 'Author Email';
		$fields['Status'] = 'Post Status';
		$fields['Meta'] = 'Meta Tag Information';
		$fields['Keyword'] = 'Page Keywords';
		$fields['Description'] = 'Page Description';
		$fields['CommentNotification'] = 'Comment Notification';
		$fields['Comments'] = 'Comments';
		$fields['PasswordProtected'] = 'Password for blog';
		$fields['Comments'] = 'Comments';
		$fields['TagClouds'] = 'Tag Clouds';
		$fields['blog_date'] = 'blog date';
		$fields['blog_time'] = 'blog time';
		$fields['Summary'] = 'Summary';
		$fields['ccat'] = 'Summary';
		$this->validation->set_fields($fields);
		
		if (empty($_POST))
		{
		   $this->validation->blog_date=date('Y-m-d');
		   $this->validation->blog_time=date('H:i');
		   $this->validation->Author=$this->session->userdata('username');
		   $this->validation->AuthorEmail=$this->session->userdata('email');
		   $data['cats']=$this->Categories_model->CheckboxCategories();
		}
		else
		{
			$data['cats']=$this->Categories_model->CheckboxCategories($this->validation->ccat);
		}
        
		if ($this->validation->run()) 
		{
			$this->Posts_model->create_blog();
	    } 
		else 
		{
           	$data['title']="Create a new blog - Blogmer";
			$data['content']=$this->load->view('admin/blog_add',$data,TRUE);
			$this->load->view('admin/main',$data);
        }
		
	}
	
	function ManageBlogs($page_id=0)
	{
		$data['title']="Blog Management - Blogmer";
		$data['content']=$this->Posts_model->list_posts($page_id);
		$this->load->view('admin/main',$data);
	}
	function DeleteBlog($post_id,$page_id,$post_title)
	{
		$this->Posts_model->delete_post($post_id,$page_id,$post_title);
	}
	
	function EditBlog($post_id,$page_id=1)
	{
		//Some posted items are required
		$rules['Title'] = "required";
		$rules['Content'] = "required";
		$rules['Status'] = "required";
		$rules['blog_time'] = 'required';
		$rules['blog_date'] = 'required';
		$this->validation->set_rules($rules);
		
		//Text for posted items. 
		$fields['Title'] = 'Post Title';
		$fields['Content'] = 'Post Content';
		$fields['Author'] = 'Author Name';
		$fields['AuthorEmail'] = 'Author Email';
		$fields['Status'] = 'Post Status';
		$fields['Meta'] = 'Meta Tag Information';
		$fields['Keyword'] = 'Page Keywords';
		$fields['Description'] = 'Page Description';
		$fields['CommentNotification'] = 'Comment Notification';
		$fields['Comments'] = 'Comments';
		$fields['PasswordProtected'] = 'Password for blog';
		$fields['Comments'] = 'Comments';
		$fields['TagClouds'] = 'Tag Clouds';
		$fields['PostedDate'] = 'Posted date';
		$fields['Summary'] = 'Summary';
		$fields['blog_time'] = 'Time';
		$fields['blog_date'] = 'Date';
	
		if (empty($_POST))
		{
		   $this->db->where(array('PostID'=>$post_id));
			$query=$this->db->get('posts');
			$post=$query->row_array();	
			
			$this->validation->Title = $post['Title'];
			$this->validation->PageLabel = $post['Label'];
			$this->validation->Content = $post['Content'];
			$this->validation->Status = $post['Status'];
			$this->validation->Meta = $post['Meta'];
			$this->validation->Keyword = $post['Keyword'];
			$this->validation->Description = $post['Description'];
			if($post['CommentNotification']==1)
				$this->validation->set_checkbox('CommentNotification', '1');
			else
				$this->validation->set_checkbox('CommentNotification', '0')	;  
			
			if($post['Comments']==1)
				$data['Comments']='checked="checked"';
			else
				$data['Comments']='';	  
			
			$this->validation->PasswordProtected = $post['PasswordProtected'];
			$this->validation->Comments = $post['Comments'];
			$this->validation->TagClouds = $post['TagClouds'];
			$this->validation->Summary = $post['Summary'];
			$this->validation->Author = $post['Author'];
			$this->validation->AuthorEmail = $post['AuthorEmail'];
			$this->validation->blog_date = substr($post['PostedDate'],0,10);
			$this->validation->blog_time = substr($post['PostedDate'],11,5);
			
		} 	
		
		if ($this->validation->run()) 
		{
        	$this->Posts_model->edit_blog($post_id,$page_id=1);
		} 
		else 
		{
          	$data['cats']=$this->Categories_model->CheckboxCategories();
			$data['title']="Edit a blog - Blogmer";
			$data['content']=$this->load->view('admin/blog_edit',$data,TRUE);
			$this->load->view('admin/main',$data);
        }

	}
	
	/*********************** end of posts functions ************************************/
	
	/**********************************************************************************/
	/***************************** Pages functions ************************************/
	
	
	function CreatePage()
	{
		//Some posted items are required
		$rules['Title'] = "required";
		$rules['PageLabel'] = "required";
		$rules['Content'] = "required";
		$rules['Status'] = "required";
		$this->validation->set_rules($rules);

		//Text for posted items. 
		$fields['Title'] = 'Page Title';
		$fields['PageLabel'] = 'Page Label(text)';
		$fields['Content'] = 'Page Content';
		$fields['Status'] = 'Page Status';
		$fields['Meta'] = 'Meta Tag Information';
		$fields['Keyword'] = 'Page Keywords';
		$fields['Description'] = 'Page Description';
		$this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
        	$this->Pages_model->create_page();
		} 
		else 
		{
        	$data['title']="Create a new page - Blogmer";
			$data['content']=$this->load->view('admin/page_add',$data,TRUE);
			$this->load->view('admin/main',$data);
        }
		
	}
	 
	function ManagePages($page_id=0)
	{
			$login = $this->session->userdata('blogmer_admin_logged_in');
			if(!$login)
			{		 redirect('admin/login','refresh'); }
		
			$data['title']="Manage pages - Blogmer";
			$data['content']=$this->Pages_model->list_pages($page_id);
			$this->load->view('admin/main',$data);
	}
	 
	function DeletePage($id,$page_id)
	{
			$login = $this->session->userdata('blogmer_admin_logged_in');
			if(!$login)
			{		 redirect('admin/login','refresh'); }
		
			$this->Pages_model->delete_page($id,$page_id);
	}
	
	function EditPage($blogpage_id,$page_id=1)
	{
		//Some posted items are required
		$rules['Title'] = "required";
		$rules['PageLabel'] = "required";
		$rules['Content'] = "required";
		$rules['Status'] = "required";
		$this->validation->set_rules($rules);
		
		//Text for posted items. 
		$fields['Title'] = 'Page Title';
		$fields['PageLabel'] = 'Page Label(text)';
		$fields['Content'] = 'Page Content';
		$fields['Status'] = 'Page Status';
		$fields['Meta'] = 'Meta Tag Information';
		$fields['Keyword'] = 'Page Keywords';
		$fields['Description'] = 'Page Description';
		$this->validation->set_fields($fields);

		if (empty($_POST))
		{
		   $this->db->where(array('PageID'=>$blogpage_id));
			$query=$this->db->get('pages');
			$page=$query->row_array();	
			
			$this->validation->Title = $page['Title'];
			$this->validation->PageLabel = $page['Label'];
			$this->validation->Content = $page['Content'];
			$this->validation->Status = $page['Status'];
			$this->validation->Meta = $page['Meta'];
			$this->validation->Keyword = $page['Keyword'];
			$this->validation->Description = $page['Description'];
		} 			
		   
		if ($this->validation->run()) 
		{
        	$this->Pages_model->edit_page($blogpage_id,$page_id);
		} 
		else 
		{
        	$data['title']="Update a page - Blogmer";
			$data['content']=$this->load->view('admin/page_edit',$data,TRUE);
			$this->load->view('admin/main',$data);
		}
		
	}
	
	/*********************** end of page functions ************************************/
	
	/**********************************************************************************/
	/***************************** Users functions ************************************/
	

	function ManageUsers($page_id=1)
	{
		$data['title']="Users Management -Blogmer";
		$data['content']=$this->Users_model->list_users($page_id);
		$this->load->view('admin/main',$data);
	}
	 
	function CreateUser()
	{
		$rules['username'] = "trim|required|min_length[4]|max_length[20]|callback__check_username";
		$rules['email'] = "trim|required|valid_email|max_length[100]";
		$rules['password'] = "trim|required|min_length[6]|max_length[20]";
		$rules['confirmpassword']="trim|matches[password]";
		$rules['firstname'] = "required";
		$rules['murl'] = "required";
		$rules['lastname'] = "required";
		
		$this->validation->set_rules($rules);

		$fields['username'] = 'Username';
        $fields['email'] = 'Email ID';
		$fields['murl'] = 'URL - Website link';
        $fields['firstname'] = 'First Name';
		$fields['lastname'] = 'Last Name';
        $fields['password'] = 'Password';
        $fields['confirmpassword'] = 'Confirm Password';
		 $fields['membergroup'] = 'Member Group';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
				$this->Users_model->create_user();   
		}
		else 
		{
          	$data['title']="Create a new user -Blogmer";
			$data['content']=$this->load->view('admin/user_add',$data,TRUE);
			$this->load->view('admin/main',$data);	
        }
		
		
	}
	
	function DeleteUser($id,$page_id)
	{
		$this->Usermodel->delete_user($id,$page_id);
	}
	
	function EditUser($user_id,$page_id=1)
	{
		$rules['username'] = "trim|required|min_length[4]|max_length[20]";
		$rules['email'] = "trim|required|valid_email|max_length[100]";
		$rules['password'] = "trim|min_length[6]|max_length[20]";
		$rules['confirmpassword']="trim|matches[password]";
		$rules['firstname'] = "required";
		$rules['murl'] = "required";
		$rules['lastname'] = "required";
		
		$this->validation->set_rules($rules);
		
		if ($this->validation->run()) 
		{     	
			
			$this->Users_model->edit_user($user_id,$page_id);        
		} 
		else 
		{
	        $this->db->where(array('MemberID'=>$user_id));
			$query=$this->db->get('users');
			$data['user']=$query->row_array();	
			$data['title']="Edit User - Blogmer";
			$data['content']=$this->load->view('admin/user_edit',$data,TRUE);
			$this->load->view('admin/main',$data);
        }
	}
	
	function _check_username($username) 
	{
        $email = $this->input->post('email');
        $query = $this->db->getwhere('users', array('MName'=>$username), 1, 0);
        if ($query->num_rows() != 0) 
		{
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        } 
		elseif($email!="") 
		{
            $query = $this->db->getwhere('users', array('MEmail'=>$email), 1, 0);
            if ($query->num_rows() != 0) 
			{
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }
	function ActivateUser($id,$page_id)
	{
		$this->Users_model->activate_user($id,$page_id);
	}
	
	function DeactivateUser($id,$page_id)
	{
		$this->Users_model->deactivate_user($id,$page_id);
	}
	
	/*********************** end of user functions ************************************/
	
	/***********************************************************************************/
	/***************************** Categories functions ********************************/
	
	function CreateCategory()
	{

		$rules['category'] = "required";
		$this->validation->set_rules($rules);

		$fields['category'] = "Category";
		$this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{     	
			$this->Categories_model->save_category();        
		} 
		else 
		{
           	$data['categories']=$this->Categories_model->categories_list();
			$data['title']="Create Category - Blogmer";
			$data['content']=$this->load->view('admin/cat_add',$data,TRUE);
			$this->load->view('admin/main',$data);

        }

	}
	
	function ManageCategories($page_id=1)
	{
		$data['title']="Category Management - Blogmer";
		$data['content']= $this->Categories_model->list_categories($page_id);
		$this->load->view('admin/main',$data);
	}
	
	function EditCategory($cat_id,$page_id=1)
	{
		$rules['category'] = "required";
		$this->validation->set_rules($rules);

		$fields['category'] = "Category";
		$this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{     	
			$this->Categories_model->edit_category($cat_id,$page_id);        
		} 
		else 
		{
     		$data['categories']=$this->Categories_model->categories_list();
			$data['cat']=$this->Categories_model->find_cat($cat_id);
			$data['title']="Blogmer";
			$data['content']= $this->load->view('admin/cat_edit',$data,TRUE);

			$this->load->view('admin/main',$data);
        }
		
	}
	
	function DeleteCategory($cat_id,$page_id=1)
	{
		$this->Categories_model->delete_category($cat_id,$page_id);
	}
	 
	
	/*********************** end of categories functions *******************************/
	
	/***********************************************************************************/
	/***************************** Comments functions **********************************/
		
	function ManageComments($page_id=1)
	{
		$data['title']="Comment Management - Blogmer";
		$data['content']= $this->Comments_model->ListAllComments($page_id);
		$this->load->view('admin/main',$data);
	}
	
	function EditComment($comment_id,$page_id=1)
	{
		$rules['name'] = "required";
		$rules['commentbox'] = "required";
		$this->validation->set_rules($rules);

		
		if ($this->validation->run()) 
		{     	
			$this->Comments_model->UpdateComment($comment_id,$page_id);        
		} 
		else 
		{
            //load the form again with previous value
			$new['comment_data']=$this->Comments_model->EditLoadComment($comment_id);
			$data['title']="Edit Comment - Blogmer";
			$data['content']=$this->load->view('admin/comment_edit',$new,TRUE);
			$this->load->view('admin/main',$data);			

        }
	}
	
	function DeleteComment($comment_id,$page_id=1)
	{
		$this->Comments_model->DeleteComment($comment_id,$page_id);
	}
	
	/*********************** end of comments functions ************************************/
	
	/**********************************************************************************/
	/***************************** Themes functions ************************************/
	
	
	function Themes()
	{
		$data['title']="Themes and Layout - Blogmer";
		$data['content']=$this->load->view('admin/themes',$data,TRUE);
		$this->load->view('admin/main',$data);	
	}
	function SaveTheme($theme_folder,$theme_name)
	{
		$this->Settings_model->SaveThemeSettings($theme_folder,$theme_name);	
	}
	
	/*********************** end of themes functions ************************************/
	
	/************************************************************************************/
	/***************************** General functions ************************************/
	
	function License()
	{
		$data['title']="License - Blogmer";
		$data['content']=$this->load->view('general/license',$data,TRUE);
		$this->load->view('admin/main',$data);
	}
	
	function Credits()
	{
		$data['title']="Credits - Blogmer";
		$data['content']=$this->load->view('general/team',$data,TRUE);
		$this->load->view('admin/main',$data);
	}
	
	function Help()
	{
		$data['title']="Help - Blogmer";
		$data['content']=$this->load->view('general/help',$data,TRUE);
		$this->load->view('admin/main',$data);
	}
	
	function About()
	{	
		$data['title']="About - Blogmer";
		$data['content']=$this->load->view('general/about',$data,TRUE);
		$this->load->view('admin/main',$data);
	}
	
	function Report_Bug()
	{
		//$data['title']="Report Bug - Blogmer";
		//$data['content']=$this->load->view('required/report',$data,TRUE);
		//$this->load->view('admin/main',$data);
		
	}
	
	/*********************** end of general functions ************************************/

	
}

